package cn.tedu.submarine_2;

import javax.swing.ImageIcon;
import java.awt.Graphics;
import java.util.Random;

public abstract class SeaObject {
    public static final int LIVE=0;
    public static final int DEAD=1;
    protected int state=LIVE;
    protected int width;
    protected int height;
    protected int x;
    protected int y;
    protected int speed;

    /**
     * 潜艇对象宽高不一样，不能写死，x,y,speed代码一直，可以写死
     * @param width
     * @param height
     */
    public SeaObject(int width,int height){
        this.width=width;
        this.height=height;
        x=-width;
        Random rand=new Random();
        y=rand.nextInt(World.HEIGHT-height-150+1)+150;
        speed= rand.nextInt(3)+1;
    }

    /**
     * 战舰，水雷，炸弹x,y,speed宽高不一样，不能写死
     * @param width
     * @param height
     * @param x
     * @param y
     * @param speed
     */
    public SeaObject(int width,int height,int x,int y,int speed){
        this.width=width;
        this.height=height;
        this.x=x;
        this.y=y;
        this.speed=speed;
    }

    /**
     * 海洋对象移动
     */
    public abstract void move();

    /**
     * 获取对象图片
     * @return 返回对象所对应图片
     */
    public abstract ImageIcon getImage();

    /**
     * 判断对象是否是活着的
     * @return 返回true，代表活着
     */
    public boolean isLive(){
        return state==LIVE;
    }
    /**
     * 判断对象是否是死了的
     * @return 返回true，代表死了
     */
    public boolean isDead(){
        return state==DEAD;
    }

    /**
     * 画对象
     * @param g
     */
    public void paintImage(Graphics g){
        if(this.isLive()){
            this.getImage().paintIcon(null,g,this.x,this.y);
        }
    }

    /**
     * 判断是否越界
     * @return
     */
    public boolean isOutOfBounds(){
        return this.x>=World.WIDTH;
    }

    /**
     * 检测是否碰撞
     * @param other
     * @return
     */
    public boolean isHit(SeaObject other){
        int x1=this.x-other.width;//潜艇的x-炸弹的宽
        int x2=this.x+other.width;//潜艇的x+潜艇的宽
        int y1=this.y-other.height;//潜艇的y-炸弹的高
        int y2=this.y+other.height;//潜艇的y+潜艇的gao
        int x=other.x;//炸弹的宽
        int y=other.y;//炸弹的高
        return x>=x1 && x<=x2
                &&
                y>=y1 && y<=y2; //x在x1与x2之间，并且，y在y1与y2之间，即为撞上了

    }
    public void goDead(){
        state = DEAD; //将当前状态修改为DEAD死了的
    }

}
